package com.example.appointment.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.appointment.AppointmentApplication;
import com.example.appointment.R;
import com.example.appointment.meta.CitiesBean;
import com.example.appointment.meta.MyCollectionSetVO;
import com.example.appointment.util.Constant;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.ArrayList;

/**
 * 我的收藏（系列）的适配器
 * Created by hehe on 2016/7/5.
 */
public class MyCollectionSetAdapter extends RecyclerView.Adapter<MyCollectionSetAdapter.MyViewHolder> {
    private Context context;
    private ArrayList<MyCollectionSetVO> arrayList;
    private Handler mHander;
    private boolean isdelete = false;//判断是否是删除的布尔值

    public void setIsdelete(boolean isdelete) {
        this.isdelete = isdelete;
        notifyDataSetChanged();
    }

    public void removeitem(CitiesBean item) {
        int index = arrayList.indexOf(item);
        arrayList.remove(item);
        notifyItemRemoved(index);
    }

    public void clearall() {
        arrayList.clear();
        notifyDataSetChanged();
    }

    /**
     * 替换目标对象
     *
     * @param item
     */
    public void setitem(MyCollectionSetVO item) {
        int index = 0;
        for (int i = 0; i < this.arrayList.size(); i++) {
            if (arrayList.get(i).getId() == item.getId()) {
                arrayList.set(i, item);
                index = i;
            }
        }
        notifyItemChanged(index);
    }

    public void additem(MyCollectionSetVO item) {

        this.arrayList.add(item);
        int index = arrayList.indexOf(item);
        notifyItemInserted(index);
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
                context).inflate(R.layout.item_collection_set, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        final MyCollectionSetVO itemvo = arrayList.get(position);
        if (isdelete) {
            //如果是删除
            holder.deleteimg.setVisibility(View.VISIBLE);
            holder.helpview.setVisibility(View.GONE);
        } else {
            //如果不是是删除
            holder.deleteimg.setVisibility(View.GONE);
            holder.helpview.setVisibility(View.VISIBLE);
        }
        if (itemvo.isselect()) {
            holder.deleteimg.setImageResource(R.mipmap.check_circle);

        } else {
            holder.deleteimg.setImageResource(R.mipmap.circle);
        }
        holder.titleview.setText(itemvo.getVideotitle());
        holder.videoinfo.setText("已收藏" + itemvo.getVodeototalnum() + "集" + "/" + "共" + itemvo.getTotalfavorite() + "集");
        AppointmentApplication.getImageLoader().displayImage("http://bbs.xdnice.com/UploadFile/2005-8/20058183241570486.jpg", holder.bgimg,
                new DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.qcloud_player_icon_audio_vol)
                        .showImageOnFail(R.drawable.qcloud_player_icon_audio_vol).cacheOnDisk(true).cacheInMemory(true).imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
                        .build());

        holder.clicklayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isdelete) {
                    if (itemvo.isselect()) {
                        //判断是否是选中状态
                        itemvo.setIsselect(false);
                        setitem(itemvo);
                    } else {
                        //判断是否是选中状态
                        itemvo.setIsselect(true);
                        setitem(itemvo);
                    }
                } else {
                    Toast.makeText(context, "你点击了" + itemvo.getVideotitle(), Toast.LENGTH_LONG).show();
                }

            }
        });
        holder.clicklayout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Message msg = new Message();
                msg.what = Constant.ONITEM_LONG_CLICK;
                msg.obj = itemvo;
                mHander.sendMessage(msg);
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    public ArrayList<MyCollectionSetVO> getArrayList() {
        return arrayList;
    }

    public void setArrayList(ArrayList<MyCollectionSetVO> arrayList) {
        this.arrayList = arrayList;
    }

    public MyCollectionSetAdapter(Context context, Handler mHander) {
        this.context = context;
        this.mHander = mHander;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        LinearLayout clicklayout;//点击的布局
        View helpview;//填充用品
        ImageView deleteimg;//删除按钮
        TextView titleview;//标题
        TextView videoinfo;//系列的集数详情
        ImageView bgimg;//背景图片

        public MyViewHolder(View itemView) {
            super(itemView);
            helpview = itemView.findViewById(R.id.item_collection_type1_view);
            deleteimg = (ImageView) itemView.findViewById(R.id.item_collection_delete);
            titleview = (TextView) itemView.findViewById(R.id.item_collection_title);
            videoinfo = (TextView) itemView.findViewById(R.id.item_collection_videoinfo);
            bgimg = (ImageView) itemView.findViewById(R.id.item_collection_set_bg);
            clicklayout = (LinearLayout) itemView.findViewById(R.id.item_collection_set_layout);
//            item_collection_set_bg
        }
    }

}
